package com.ziqikeji.xianyan.service.user;

import com.ziqikeji.xianyan.common.BaseService;
import com.ziqikeji.xianyan.dto.user.RoleDTO;
import com.ziqikeji.xianyan.entity.user.Role;
import com.ziqikeji.xianyan.repository.user.RoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户 service
 *
 * @author liuyu
 */
@Service
public class RoleService extends BaseService<Role, RoleDao> {

    @Autowired
    private RoleDao roleDao;

    /**
     * 获取角色的DTO list(用于后台增加用户时设置角色)
     *
     * @return
     */
    public List<RoleDTO> getAllRoleDTOList() {
        List<RoleDTO> roleDTOList = new ArrayList<>();
        Iterable<Role> roleIterable = this.getAll();
        for (Role role : roleIterable) {
            RoleDTO roleDTO = new RoleDTO(role);
            roleDTOList.add(roleDTO);
        }
        return roleDTOList;
    }

    /**
     * 通过角色name获取角色
     *
     * @param name 角色 如：ROLE_USER
     * @return 角色
     */
    public Role getByName(String name) {
        return this.roleDao.findRoleByName(name);
    }
}
